package com.ruoyi.teacher.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.teacher.api.domain.TeacherDto;
import com.ruoyi.teacher.api.domain.TeacherType;
import com.ruoyi.teacher.api.domain.UserToTeacherDto;
import com.ruoyi.teacher.domain.Teacher;
import com.ruoyi.teacher.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 老师中心Controller
 *
 * @author clid
 * @date 2024-03-23
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController extends BaseController {
    @Autowired
    private ITeacherService teacherService;


    @PostMapping(value = "/loadIndexData")
    R<Map<String,Object>> loadIndexData() {
        return R.ok(teacherService.loadIndexData());
    }

    @GetMapping(value = "/loadSuperiorsObjs")
    R<List<Teacher>> loadSuperiorsObj() {
        return R.ok(teacherService.loadSuperiorsObj());
    }

    /**
     * 获取所有的老师
     *
     * @return {@link R}<{@link List}<{@link TeacherDto}>>
     */
    @GetMapping(value = "/loadTeachers")
    R<List<TeacherDto>> loadTeachers() {
        return R.ok(teacherService.loadTeachers());
    }


    /**
     * 用户对教师
     *
     * @param dto dto
     * @return {@link R}<{@link Void}>
     */
    @PostMapping(value = "/userToTeacher")
    R<Void> userToTeacher(@RequestBody UserToTeacherDto dto) {
        teacherService.userToTeacher(dto);
        return R.ok();
    }


    /**
     * 查询老师类型列表
     */
    /*@RequiresPermissions("teacher:teacher:list")*/
    @GetMapping("/teacherTypeList")
    public AjaxResult list() {
        List<TeacherType> list = teacherService.selectTeacherTypeList();
        return AjaxResult.success(list);
    }

    /**
     * 查询老师中心列表
     */
    @RequiresPermissions("teacher:teacher:list")
    @GetMapping("/list")
    public TableDataInfo list(Teacher teacher) {
        startPage();
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        return getDataTable(list);
    }

    /**
     * 导出老师中心列表
     */
    @RequiresPermissions("teacher:teacher:export")
    @Log(title = "老师中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Teacher teacher) {
        List<Teacher> list = teacherService.selectTeacherList(teacher);
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        util.exportExcel(response, list, "老师中心数据");
    }

    @PostMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<Teacher> list = new ArrayList<>();
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        util.exportExcel(response, list, "老师中心数据");
    }

    /**
     * 导入老师中心列表
     */

    @PostMapping("/import")
    public R<Void> importTeacher(MultipartFile file) {
        try {
            ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
            List<Teacher> teachers = util.importExcel(file.getInputStream());
            teacherService.importTeacher(teachers);
        } catch (IOException e) {
            e.printStackTrace();
            return R.fail("上传失败");
        }
        return R.ok();
    }


    /**
     * 获取老师中心详细信息
     */
    @RequiresPermissions("teacher:teacher:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(teacherService.selectTeacherById(id));
    }

    /**
     * 新增老师中心
     */
    @RequiresPermissions("teacher:teacher:add")
    @Log(title = "老师中心", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Teacher teacher) {
        return toAjax(teacherService.insertTeacher(teacher));
    }

    /**
     * 修改老师中心
     */
    @RequiresPermissions("teacher:teacher:edit")
    @Log(title = "老师中心", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Teacher teacher) {
        return toAjax(teacherService.updateTeacher(teacher));
    }

    /**
     * 删除老师中心
     */
    @RequiresPermissions("teacher:teacher:remove")
    @Log(title = "老师中心", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(teacherService.deleteTeacherByIds(ids));
    }
}
